package com.zhaoyue.study.mq.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/2/21
 * Time: 17:27
 * To change this template use File | Settings | File Templates.
 */
public class Pdemo {

    public static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception{
        //创建到服务器的连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.56.56");
        //远程需要设置密码,本机可以不用设置userName和password,默认用guest
        factory.setUsername("test");
        factory.setPassword("123456");
        Connection connection = factory.newConnection();//建立连接
        Channel channel = connection.createChannel();//通道

        //声明队列（durable:是否持久性，exclusive：私有性，autoDelete：自动删除）
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //消息体
        String message = "Hello World";
        //("":默认交换器,)
        //channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
        //PERSISTENT_TEXT_PLAIN 标志消息是持久化的,否则可以传null
        channel.basicPublish("",QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
        System.out.println("send done");

        channel.close();
        connection.close();
    }
}
